package cms.simmytech.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cms.simmytech.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cms.simmytech.web.base.BaseController;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {

	@RequestMapping(value = "/userList")
	public String userList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
						   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Model model) {
		PageInfo<SysUser> pageInfo = userService.getUserList(pageNum, pageSize);
		model.addAttribute("pageInfo", pageInfo);
		return super.toPage("user/userList", model);
	}

	@RequestMapping(value = "/userDetail")
	public String userDetail(Integer id, Model model) {
		TbBase base = super.getBase();
		SysUser bean = null;
		SysUserBase  userBase=null;
		SysRole role=new SysRole();
		TbExaminer examiner=new TbExaminer();
		int roleId = -1;
		if (id == null) {
			bean = new SysUser();
			userBase=new SysUserBase();
			userBase.setBaseId(0);
		} else {
			bean = userService.getUser(id);
			roleId = userService.getRoleIdByUid(id);
			role = userService.getRoleByUid(id);
			userBase=userService.getUserBase(id);
		}
		List<SysRole> roleList = userService.getRoleList();
		model.addAttribute("roleId", roleId);
		model.addAttribute("roleList", roleList);
		model.addAttribute("userBase", userBase);
		model.addAttribute("bean", bean);
		model.addAttribute("role", role);
		model.addAttribute("examiner", examiner);
		return super.toPage("user/userDetail", model);
	}

	@PostMapping(value = "/userSave")
	public String userSave(Integer id, String username, String password, int roleId, int state, Integer baseId,Integer projectId,String  examinerName, Model model) {
		SysUser bean = new SysUser();
		bean.setId(id);
		bean.setPassword(password);
		bean.setUsername(username);
		bean.setState(state);
		userService.saveUser(bean, roleId, baseId,projectId,examinerName);
		return "redirect:userList";
	}

	@RequestMapping(value = "/checkUserName")
	@ResponseBody
	public Map<String, Object> checkUserName(Integer id, String username) {
		Map<String, Object> map = new HashMap<>();
		map.put("res", userService.checkUserName(id, username));
		return map;
	}

	@RequestMapping(value = "/userDelete")
	@ResponseBody
	public Map<String, Object> userDelete(int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		userService.deleteUser(id);
		map.put("res", true);
		return map;
	}

	@RequestMapping(value = "/menuList")
	public String menuList(@RequestParam(value = "pId", defaultValue = "0") Integer pId, Model model) {
		List<SysPermission> list = userService.getPermissionList(pId);
		if (pId == 0) {
			model.addAttribute("baseId", 0);
		} else {
			SysPermission bean = userService.getPermission(pId);
			model.addAttribute("baseId", bean.getPid());
		}
		model.addAttribute("list", list);
		model.addAttribute("pId", pId);

		return super.toPage("user/menuList", model);
	}

	@RequestMapping(value = "/menuDetail")
	public String menuAdd(int pId, Integer id, Model model) {
		SysPermission bean = null;
		if (id == null) {
			bean = new SysPermission();
			bean.setPid(pId);
		} else {
			bean = userService.getPermission(id);
		}
		model.addAttribute("bean", bean);
		return super.toPage("user/menuDetail", model);
	}

	@RequestMapping(value = "/menuDelete")
	@ResponseBody
	public Map<String, Object> menuDelete(int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		userService.deletePermission(id);
		map.put("res", true);
		return map;
	}

	@PostMapping(value = "/menuSave")
	public String menuSave(Integer id, String name, String descritpion, String url, int isShow, int pId, String legend,Model model) {
		SysPermission bean = new SysPermission();
		bean.setId(id);
		bean.setName(name);
		bean.setDescritpion(descritpion);
		bean.setUrl(url);
		bean.setIsShow(isShow);
		bean.setPid(pId);
		bean.setLegend(legend);
		userService.savePermission(bean);
		return "redirect:menuList?pId=" + pId;
	}

	@RequestMapping(value = "/moveMenu")
	@ResponseBody
	public Map<String, Object> movePermission(int id, String type) {
		Map<String, Object> map = new HashMap<>();
		map.put("res", userService.movePermission(id, type));
		return map;
	}

	@RequestMapping(value = "/roleList")
	public String RoleList(Model model) {
		List<SysRole> list = userService.getRoleList();
		model.addAttribute("list", list);
		return super.toPage("user/roleList", model);
	}

	@RequestMapping(value = "/roleAdd")
	public String roleAdd(Model model) {
		List<SysPermission> list = userService.getPermissionList();
		StringBuilder sBuilder = new StringBuilder();
		for (SysPermission menu : list) {
			sBuilder.append("{id : " + menu.getId() + ",pId : " + menu.getPid() + ",name :\"" + menu.getName() + "\",open : true},");
		}
		String strMenu = sBuilder.substring(0, sBuilder.length() - 1);
		model.addAttribute("strMenu", "[" + strMenu + "]");
		return super.toPage("user/roleAdd", model);
	}

	@RequestMapping(value = "/roleDetail")
	public String roleUpdate(int roleId, Model model) {
		SysRole bean = userService.getRole(roleId);
		List<SysPermission> list = userService.getPermissionList();
		List<SysPermissionRole> checkList = userService.getRoleHas(roleId);
		StringBuilder sBuilder = new StringBuilder();
		for (SysPermission menu : list) {
			boolean temp = false;
			for (SysPermissionRole vo : checkList) {
				if (menu.getId().intValue() == vo.getPermissionId().intValue()) {
					sBuilder.append("{id : " + menu.getId() + ",pId : " + menu.getPid() + ",name : \"" + menu.getName()
							+ "\",open : true,checked:true},");
					temp = true;
				}
			}
			if (!temp) {
				sBuilder.append("{id : " + menu.getId() + ",pId : " + menu.getPid() + ",name : \"" + menu.getName() + "\",open : true},");
			}
		}
		String strMenu = sBuilder.substring(0, sBuilder.length() - 1);
		model.addAttribute("strMenu", "[" + strMenu + "]");
		model.addAttribute("bean", bean);
		return super.toPage("user/roleDetail", model);
	}

	@PostMapping(value = "/roleSave")
	public String roleSave(Integer id, String name, String descritpion, String menuIds) {
		SysRole bean = new SysRole();
		bean.setId(id);
		bean.setName(name);
		bean.setDescritpion(descritpion);
		String[] menus = menuIds.split(",");
		int[] ids = new int[menus.length];
		for (int i = 0; i < ids.length; i++) {
			ids[i] = Integer.parseInt(menus[i]);
		}
		userService.saveRole(bean, ids);
		return "redirect:roleList";
	}

	@RequestMapping(value = "/roleDelete")
	@ResponseBody
	public Map<String, Object> roleDelete(int roleId) {
		Map<String, Object> map = new HashMap<>();
		int res = userService.deleteRole(roleId);
		if (res==1){
			map.put("mess", "删除成功");
		}else {
			map.put("mess", "删除失败,该角色下存在用户");
		}
		map.put("res", true);
		return map;
	}
}
